using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using PlugNT.Safe;
using PlugNT.Common;

namespace PlugNT.Cms.Custom
{
    public class SysManager
    {
        private static object objFlag = new object();
        private static long onlyFlag = 0;
        private static string nowFlag = "null";

        static SysManager()
        {
            nowFlag = DateTime.Now.ToString("yyyyMMddhhmmssfff");
        }




        public static string LogErrorPath
        {
            get { return WebConfig.RootPath + @"Logs\Error\" + DateTime.Now.ToShortDateString(); }
        }





        public static string GetNoString()
        {

            lock (objFlag)
            {
                string tmpFlag = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                if (nowFlag == tmpFlag)
                {
                    onlyFlag++;
                }
                else
                {
                    onlyFlag = 0;
                    nowFlag = tmpFlag;
                }
                return (tmpFlag + onlyFlag.ToString("000")); 
            }
        }





        public static string GetLongNoString()
        {
            string sessionid = (System.Web.HttpContext.Current.Session == null) ? "" : System.Web.HttpContext.Current.Session.SessionID.GetHashCode().ToString();
            return PlugNT.Safe.Encrypter.MD5(Utils.GetDateTimeCode() + sessionid);
        }








        public static void LogErrorWriteln(string text)
        {
            string dic = LogErrorPath;
            string logfilepath = dic + @"\" + GetNoString() + ".txt";
            if (!Directory.Exists(dic))
            {
                Directory.CreateDirectory(dic);
            }
            if (File.Exists(logfilepath))
            {
                StreamWriter sr = File.AppendText(logfilepath);
                sr.WriteLine("\n" + DateTime.Now.ToString() + ":\n");
                sr.WriteLine(text);
                sr.Close();
            }
            else
            {
                StreamWriter sr = File.CreateText(logfilepath);
                sr.WriteLine("\n" + DateTime.Now.ToString() + ":\n");
                sr.WriteLine(text);
                sr.Close();
            }
        }


    }
}

